﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Po;
using nERP.Model;
using nERP.Model.Bas;
using nERP.Model.Wms;

namespace nERP.ViewModel.Po.BuyReturnVMs
{
    public partial class BuyReturnListVM : BasePagedListVM<BuyReturn_View, BuyReturnSearcher>
    {

        protected override IEnumerable<IGridColumn<BuyReturn_View>> InitGridHeader()
        {
            return new List<GridColumn<BuyReturn_View>>{

                this.MakeGridHeader(x => x.BuyReturn_BuyReturnCode).SetTitle(@Localizer["Page.退货单号"].Value),
                this.MakeGridHeader(x => x.BuyReturn_BuyInWarehouse).SetTitle(@Localizer["Page.采购入库"].Value),
                this.MakeGridHeader(x => x.BuyReturn_Buy).SetTitle(@Localizer["Page.采购单号"].Value),
                this.MakeGridHeader(x => x.BuyReturn_Supplier).SetTitle(@Localizer["Page.供应商"].Value),
                this.MakeGridHeader(x => x.BuyReturn_InvoicesDate).SetTitle(@Localizer["Page.单据日期"].Value),
                this.MakeGridHeader(x => x.BuyReturn_MakeUser).SetTitle(@Localizer["Page.制单人"].Value),
                this.MakeGridHeader(x => x.BuyReturn_Status).SetTitle(@Localizer["Page.单据状态"].Value),
                this.MakeGridHeader(x => x.BuyReturn_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.BuyReturn_TotalPrice).SetTitle(@Localizer["Page.总金额"].Value),
                this.MakeGridHeader(x => x.BuyReturn_TotalPrice1).SetTitle(@Localizer["Page.已结金额"].Value),
                this.MakeGridHeader(x => x.BuyReturn_TotalPrice2).SetTitle(@Localizer["Page.未结金额"].Value),
                this.MakeGridHeader(x => x.BuyReturn_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.BuyReturn_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.BuyReturn_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.BuyReturn_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),
                this.MakeGridHeader(x => x.BuyReturn_IsValid).SetTitle(@Localizer["_Admin.IsValid"].Value),
                this.MakeGridHeader(x => "wtmtemp_CanApprove").SetHide().SetFormat((a, b) =>
                {
                    if(a.wtmtemp_CanApprove) { return "true"; }
                    else { return "false"; }
                }),


                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<BuyReturn_View> GetSearchQuery()
        {
            var query = DC.Set<BuyReturn>()

                .CheckContain(Searcher.BuyReturnCode, x => x.BuyReturnCode)
                .CheckEqual(Searcher.BuyInWarehouseId, x => x.BuyInWarehouseId)
                .CheckEqual(Searcher.BuyId, x => x.BuyId)
                .CheckEqual(Searcher.SupplierId, x => x.SupplierId)
                .CheckBetween(Searcher.InvoicesDate?.GetStartTime(), Searcher.InvoicesDate?.GetEndTime(), x => x.InvoicesDate, includeMax: false)
                .CheckEqual(Searcher.MakeUserId, x => x.MakeUserId)
                .CheckEqual(Searcher.Status, x => x.Status)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .CheckEqual(Searcher.TotalPrice, x => x.TotalPrice)
                .CheckEqual(Searcher.TotalPrice1, x => x.TotalPrice1)
                .CheckEqual(Searcher.TotalPrice2, x => x.TotalPrice2)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x => x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x => x.UpdateBy)
                .Select(x => new BuyReturn_View
                {
                    ID = x.ID,

                    BuyReturn_BuyReturnCode = x.BuyReturnCode,
                    BuyReturn_BuyInWarehouse = DC.Set<BuyInWarehouse>().Where(z0 => z0.BuyInWarehouseCode == x.BuyInWarehouseId).Select(y => y.BuyInWarehouseCode).FirstOrDefault(),
                    BuyReturn_Buy = DC.Set<Buy>().Where(z0 => z0.BuyCode == x.BuyId).Select(y => y.BuyCode).FirstOrDefault(),
                    BuyReturn_Supplier = DC.Set<Supplier>().Where(z0 => z0.SupplierCode == x.SupplierId).Select(y => y.SupplierName).FirstOrDefault(),
                    BuyReturn_InvoicesDate = x.InvoicesDate,
                    BuyReturn_MakeUser = DC.Set<FrameworkUser>().Where(z0 => z0.ITCode == x.MakeUserId).Select(y => y.Name).FirstOrDefault(),
                    BuyReturn_Status = x.Status,
                    BuyReturn_Remark = x.Remark,
                    BuyReturn_TotalPrice = x.TotalPrice,
                    BuyReturn_TotalPrice1 = x.TotalPrice1,
                    BuyReturn_TotalPrice2 = x.TotalPrice2,
                    BuyReturn_CreateTime = x.CreateTime,
                    BuyReturn_UpdateTime = x.UpdateTime,
                    BuyReturn_CreateBy = x.CreateBy,
                    BuyReturn_UpdateBy = x.UpdateBy,
                    BuyReturn_IsValid = x.IsValid,
                })
                .OrderBy(x => x.ID);
            return query;
        }


        public override void AfterDoSearcher()
        {
            if (Wtm.LoginUserInfo != null)
            {
                var ids = GetMyApproves();
                foreach (var item in EntityList)
                {
                    var m = ids.Find(x => x.ModelID == item.GetID().ToString());
                    if (m != null)
                    {
                        item.wtmtemp_CanApprove = true;
                    }
                }
            }
        }


    }
    public class BuyReturn_View : BuyReturn
    {

        public string BuyReturn_BuyReturnCode { get; set; }
        public string BuyReturn_BuyInWarehouse { get; set; }
        public string BuyReturn_Buy { get; set; }
        public string BuyReturn_Supplier { get; set; }
        public DateTime? BuyReturn_InvoicesDate { get; set; }
        public string BuyReturn_MakeUser { get; set; }
        public StatusTypeEnum? BuyReturn_Status { get; set; }
        public string BuyReturn_Remark { get; set; }
        public decimal? BuyReturn_TotalPrice { get; set; }
        public decimal? BuyReturn_TotalPrice1 { get; set; }
        public decimal? BuyReturn_TotalPrice2 { get; set; }
        public DateTime? BuyReturn_CreateTime { get; set; }
        public DateTime? BuyReturn_UpdateTime { get; set; }
        public string BuyReturn_CreateBy { get; set; }
        public string BuyReturn_UpdateBy { get; set; }
        public bool BuyReturn_IsValid { get; set; }
        public bool wtmtemp_CanApprove { get; set; }

    }

}